<?php
/**
 * Created by PhpStorm.
 * User: luoqinghui
 * Date: 2020/7/24
 * Time: 上午17:40
 */

namespace App\Http\Controllers\Api\System;

use App\Http\Controllers\Controller;
use App\Modules\System\Services\DiningTimeService;
use App\Modules\System\Services\DictionaryService;
use App\Modules\System\Services\RemindrService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 系统设置
 */
class SettingController extends Controller{
    /**
     * 用餐时间段
     */
    public function diningTime(Request $request){
        $user_id = $this->getLoginUser()->id;
//        $user_id = 1;
        $result = DiningTimeService::getList($user_id);
        //如果没有就查询默认的
        if ( empty($result) ) {
            $code = 'diningtime';
            $dictionaryCode = DictionaryService::getDictionary($code);
            $dictionaryData = DictionaryService::getList($dictionaryCode->id);
            $result = [];
            foreach ( $dictionaryData as $key => $value ) {
                $item = [];
                $time = explode('-',$value['value']);
                $item['meals'] = (int)$value['key'];
                $item['start_time'] = $time[0];
                $item['end_time'] = $time[1];
                array_push($result,$item);
            }
        }
        return response()->success($result);
    }

    /**
     * 设置用餐时间段
     */
    public function setDiningTime(Request $request){
        $user_id = $this->getLoginUser()->id;
//        $user_id = 1;
        $dataJson = $request->input('data','');
        if ( empty($dataJson) ) return response()->error("PARAM_EMPTY");
        $dataArr = json_decode($dataJson,true);
        //获取字典里面的默认血糖值标准类型的范围
        $standard = DictionaryService::getDictionary('standard');
        $standardList = DictionaryService::getList($standard->id);
        //key对应数据 1餐前范围 2餐后范围 3睡前范围
        $standardArr = array_column($standardList,'value','key');
        $before = explode('-',$standardArr[1]);
        $after = explode('-',$standardArr[2]);
        $before_sleep = explode('-',$standardArr[3]);
        //判断用户是否已添加
        $result = DiningTimeService::getList($user_id);
        $mealsArr = array_column($result,'meals');
        DB::beginTransaction();
        try{
            foreach ($dataArr as $key => $value) {
                //没有id为添加
                if ( empty($value['id']) ) {
                    //判断是否有添加
                    if ( in_array($value['meals'],$mealsArr) ) {
                        throw new \Exception('PARAM_ERROR');
                    }
                    //添加数据
                    $value['user_id'] = $user_id;
                    //血糖标准值
                    if ( $value['meals'] == 4 ) {
                        $value['before_low'] = $before_sleep[0];
                        $value['before_high'] = $before_sleep[1];
                    }else{
                        $value['before_low'] = $before[0];
                        $value['before_high'] = $before[1];
                        $value['after_low'] = $after[0];
                        $value['after_high'] = $after[1];
                    }
                    $add = DiningTimeService::addDiningTime($value);
                    if ( empty($add) ) {
                        throw new \Exception('UPDATE_FAILURE');
                    }
                //有id为修改
                }else{
                    $value['user_id'] = $user_id;
                    $update = DiningTimeService::updateDiningTime($value);
                    if ( empty($update) && $update !== 0 ) {
                        throw new \Exception('UPDATE_FAILURE');
                    }
                }
            }
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return response()->error($e->getMessage());
        }
        return response()->success();
    }

    /**
     * 获取提醒列表
     */
    public function remindrList(){
        //        $user_id = $this->getLoginUser()->id;
        $user_id = 16;
        $remindrList = RemindrService::getList($user_id);
        return response()->success($remindrList);
    }

    /**
     * 添加提醒
     */
    public function remindrAdd(Request $request){
//        $user_id = $this->getLoginUser()->id;
        $user_id = 16;
        $type = $request->input('type');
        $music = $request->input('music');
        $plan = $request->input('plan');
        $remindr_time = $request->input('remindr_time');
        if ( empty($type) || empty($plan) || empty($remindr_time) ) return response()->error("PARAM_EMPTY");
        $insertArr = [
            'user_id'       => $user_id,
            'type'          => $type,
            'music'         => empty($music) ? '' : $music,
            'plan'          => $plan,
            'remindr_time'  => $remindr_time,
        ];
        $insert = RemindrService::add($insertArr);
        if ( empty($insert) ) response()->error("ADD_FAILURE");
        return response()->success();
    }

    /**
     * 修改提醒列表
     */
    public function remindrUpdate(Request $request){
        //        $user_id = $this->getLoginUser()->id;
        $user_id = 16;
        $id = $request->input('id');
        if ( empty($id) ) return response()->error("PARAM_EMPTY");
        $dataUpdateArr = ['id'=>$id,'user_id'=>$user_id];
        !isset($_POST['type']) ? : $dataUpdateArr['type'] = $request->input('type','');
        !isset($_POST['music']) ? : $dataUpdateArr['music'] = $request->input('music','');
        !isset($_POST['plan']) ? : $dataUpdateArr['plan'] = $request->input('plan','');
        !isset($_POST['remindr_time']) ? : $dataUpdateArr['type'] = $request->input('remindr_time','');
        !isset($_POST['status']) ? : $dataUpdateArr['status'] = $request->input('status','');
        $update = RemindrService::remindrUpdate($dataUpdateArr);
        if ( empty($update) ) response()->error("UPDATE_FAILURE");
        return response()->success();
    }
}
